package com.nightpiter.view.dialogs;

import java.util.ArrayList;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.nightpiter.view.drawer.MetroDrawer;
import com.nightpiter.view.drawer.Property;
import com.nightpiter.view.drawer.Property.Type;

public class DrawerOptionsDialog extends BaseDialog implements
		DialogInterface.OnClickListener, AdapterView.OnItemSelectedListener,
		CheckBox.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener {

	/** the subscriber that would be notified about most of events */
	protected Listener listener;

	private Property[] properties;

	private View[] tempView, radiobuttons;

	private MetroDrawer metroDrawer;

	private ArrayAdapter<String> emptyAdapter;

	protected ArrayList<ArrayAdapter<String>> ad;

	protected LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT,
			LinearLayout.LayoutParams.FILL_PARENT);

	public DrawerOptionsDialog(Context context) {
		super(context);

		this.builder.setPositiveButton("Save", this);
		this.builder.setNeutralButton("Change the Drawer", this);
		this.builder.setNegativeButton("Cancel", this);

	}

	public void show(MetroDrawer drawer) {
		this.metroDrawer = drawer;
		LayoutParams layoutParams = new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		LinearLayout linearLayout = new LinearLayout(context);
		linearLayout.setLayoutParams(layoutParams);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		linearLayout.setPadding(10, 10, 10, 10);

		Property[] oldProperties = drawer.getProperties();
		this.properties = new Property[oldProperties.length];
		for (int i = 0; i < properties.length; i++) {
			properties[i] = (Property) oldProperties[i].clone();
		}		

		if (properties != null) {
			tempView = new View[properties.length];
			int i = 0, j = 0;
			this.emptyAdapter = new ArrayAdapter<String>(getContext(),
					android.R.layout.simple_spinner_item);
			this.emptyAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			ArrayList<String> toDelete = new ArrayList<String>();
			toDelete.clear();
			ad = new ArrayList<ArrayAdapter<String>>();
			int k = 0;

			TextView tw2;

			ArrayList<String> als = new ArrayList<String>();
			for (Property property : properties) {

				if (property != null) {
					if (property.getType() == Type.CHECKBOX) {
						tempView[i] = new CheckBox(context);
						((CheckBox) tempView[i]).setTextColor(Color.WHITE);
						((CheckBox) tempView[i]).setText("   "
								+ property.getName());
						((CheckBox) tempView[i]).setChecked(property
								.getBooleanValue());
						((CheckBox) tempView[i])
								.setOnCheckedChangeListener(this);
					} else if (property.getType() == Type.RADIOGROUP) {
						tempView[i] = new RadioGroup(context);
						radiobuttons = new View[100];
						((RadioGroup) tempView[i])
								.setOnCheckedChangeListener(this);
						for (String str : property.getValues()) {
							radiobuttons[j] = new RadioButton(context);
							((RadioButton) radiobuttons[j]).setText(str);
							((RadioButton) radiobuttons[j])
									.setLayoutParams(new AbsoluteLayout.LayoutParams(
											AbsoluteLayout.LayoutParams.FILL_PARENT,
											AbsoluteLayout.LayoutParams.WRAP_CONTENT,
											j, j * 10));
							/*
							 * ragiodroups are not enabled because of some
							 * problems with layouts. use dropdown
							 * lists(spinners) instead of them=)
							 */
							j++;
						}

					} else if (property.getType() == Type.DROPDOWN_LIST) {
						tempView[i] = new Spinner(context);
						als.add(property.getName());

						((Spinner) tempView[i]).setOnItemSelectedListener(this);
						((Spinner) tempView[i]).setLayoutParams(layoutParams);
						ad.add(new ArrayAdapter<String>(getContext(),
								android.R.layout.simple_spinner_item));
						ad.get(k).setDropDownViewResource(
								android.R.layout.simple_spinner_dropdown_item);

						for (String str : property.getValues()) {

							ad.get(k).addObject(str);

						}
						((Spinner) tempView[i]).setSelection(ad.get(k)
								.getPosition(property.getCurrentValue()));
						((Spinner) tempView[i]).setPreferredWidth(200);
						((Spinner) tempView[i]).setAdapter(ad.get(k));
						k++;

					}

					tempView[i].setLayoutParams(new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.FILL_PARENT,
							LinearLayout.LayoutParams.WRAP_CONTENT));
					i++;
				}

			}

			int z = 0;
			for (View v : tempView) {
				if (v instanceof CheckBox) {
					linearLayout.addView((CheckBox) v,
							new LinearLayout.LayoutParams(
									LinearLayout.LayoutParams.FILL_PARENT,
									LinearLayout.LayoutParams.WRAP_CONTENT));

				} else if (v instanceof RadioGroup) {
					linearLayout.addView((RadioGroup) v,
							new LinearLayout.LayoutParams(
									LinearLayout.LayoutParams.FILL_PARENT,
									LinearLayout.LayoutParams.WRAP_CONTENT));

				} else if (v instanceof Spinner) {
					tw2 = new TextView(context);
					tw2.setLayoutParams(params);
					tw2.setText(als.get(z));
					tw2.setTextColor(Color.WHITE);
					linearLayout.addView(tw2);
					linearLayout.addView((Spinner) v,
							new LinearLayout.LayoutParams(
									LinearLayout.LayoutParams.FILL_PARENT,
									LinearLayout.LayoutParams.FILL_PARENT));
					z++;

				}
			}
		} else {
			Toast.makeText(context, "List of properties is empty",
					Toast.LENGTH_LONG).show();
		}
		this.builder.setTitle(drawer.getClass().getSimpleName());
		this.builder.setView(linearLayout);
		super.show();
	}

	@Override
	public void onClick(DialogInterface dialog, int button) {
		if (dialog == this.getDialog()) {
			switch (button) {
			case DialogInterface.BUTTON1:
				if (properties != null) {
					this.metroDrawer.setProperties(properties);
					((Listener) listener).onDrawerPropertiesChanged();
				} else {
					Toast.makeText(context, "There are no properties to set",
							Toast.LENGTH_LONG).show();
				}
				break;

			case DialogInterface.BUTTON2:
				break;

			case DialogInterface.BUTTON3:
				((Listener) listener).onChangeDrawerMenuItemSelected();
				break;

			default:
				break;
			}
		}
		super.onClick(dialog, button);
	}

	@SuppressWarnings("unchecked")
	public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
		int i = 0;
		for (View v : tempView) {
			if (v instanceof Spinner) {
				if (arg0 == (Spinner) v) {
					properties[i]
							.setCurrentValue(properties[i].getValues()[arg2]);
				}
			}
			i++;
		}
	}

	@SuppressWarnings("unchecked")
	public void onNothingSelected(AdapterView arg0) {
	}

	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		int i = 0;
		for (View v : tempView) {
			if (v instanceof CheckBox) {
				if (arg0 == (CheckBox) v) {
					if (((CheckBox) v).isChecked() == false) {
						properties[i].setCurrentValue("false");
					} else {
						properties[i].setCurrentValue("true");
					}
				}
			}
			i++;
		}

	}

	public void onCheckedChanged(RadioGroup arg0, int arg1) {
	}

	/**
	 * subscribe to notifications
	 * 
	 * @param listener
	 *            the listener
	 */
	public void setListener(Listener listener) {
		if (listener != null) {
			this.listener = listener;
		}
	}

	/**
	 * Listener interface.
	 * 
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 */
	public interface Listener {

		public void onChangeDrawerMenuItemSelected();

		public void onDrawerPropertiesChanged();

	}

}
